import axios  from "axios";

const request = axios.create({
    baseURL:'http://localhost:9090',
    timeout:30000
})

//request拦截器
//可以自请求发送前对请求做一些处理
request.interceptors.request.use(config => {
    config.headers['Content-Type'] = 'application/json;charset=utf-8';
    let user = JSON.parse(localStorage.getItem("user") || '{}');
    console.log(user);

    // 设置 token 到请求头
    if (user && user.token) {
        config.headers['token'] = user.token;
    }

    // 判断请求 URL 是否需要修改 baseURL
    if (config.url && (config.url.includes('/api/img/tu/boy') ||
        config.url.includes('/api/wl/yan/ming') ||
        config.url.includes('/api/zici/sjwm.php?id=88888888&key=88888888'))) {
        // 设置 baseURL 为空
        config.baseURL = '';
    }
    return config;
}, error => {
    console.error('Request error:', error);
    return Promise.reject(error);
});


request.interceptors.response.use(
    response=>{
        let res=response.data;

        if(typeof res === 'string'){
            res = res ?JSON.parse(res) : res
        }
        return res;
    },
    error => {
        console.error('response error:' + error)
        return Promise.reject(error)
    }
)

export default request